use(
    [
        'public/javascripts/service/ent_authService.js',
        'public/javascripts/widget/step/step.js',
        'public/javascripts/widget/Input/Input.js',
        'public/javascripts/widget/select/select.js',
        'public/javascripts/widget/prompt/prompt.js',
        'public/javascripts/widget/upload/upload.js',
        'public/javascripts/widget/picPreview/picPreview.js'
    ],
    function(ent_authService, StepComponent, InputComponent, selectFactory, PromptComponent, Upload, picPreview) {
        var PageComponent = React.createClass({
            muploads: null,
            componentDidMount: function() {
                var _this = this;
                picPreview.init();
                this.getAllCountry();
                this.getAllCurrency();
                this.getAllClearing();
                // this.getAllBusiness();
                this.muploads = Upload.init({
                    selector: '#m-upload-more',
                    multiple: true, //是否多组图上传
                    data:  _this.props.data.authInfo && _this.props.data.authInfo.files.length ? _this.props.data.authInfo.files : '',
                    preView: function(fileid, data) {
                        $('#slides').picturePreview({ pictureId: fileid }, data);
                        //_this.preview(data);
                    },
                    callback: function() {
                        _this.setState({ imgError: false, imgErrorTxt: '(最多上传9张图片, 单张图片最大10M)' });
                    }
                });
            },
            getInitialState: function() {
                return {
                    authSubmited: this.props.data.authSubmited,
                    authPassed: this.props.data.authPassed,
                    submitSuccess: false,
                    authInfo: this.props.data.authInfo ? this.props.data.authInfo : {},
                    countryCode: '',
                    countryList: [],
                    cityList: [],
                    imgError: false,
                    imgErrorTxt: '(最多上传9张图片, 单张图片最大10M)',
                    email: '',
                    emailError: false,
                    emailErrorTxt: '电子邮箱格式不正确'
                };
            },
            getAllCountry: function() {
                var _this = this;
                var countryEle = selectFactory({
                    data: [],
                    id: 'country',
                    isSearch: true,
                    pagination: true,
                    closeLocalSearch: true,
                    defaultText: '请选择',
                    showTextField: 'nameCode',
                    isSaveInputVal: true,
                    searchPlaceHoder: '请输入国家名称或二字码',
                    attrTextModel: function(name) {
                        var nextEle = countryEle.next;
                        nextEle.clearData();
                        _this.refs.locationCountryCode.setValue(name);
                        _this.refs.locationCityCode.setValue('');
                        var code = name.substring(name.indexOf('(') + 1, name.lastIndexOf(')'));
                        if (code === _this.state.countryCode && _this.state.cityList) {
                            cityEle.setData({ data: _this.state.cityList });
                        } else {
                            _this.setState({ countryCode: code });
                            var params = {
                                q: '',
                                countryCode: code
                            };
                            ent_authService.getAllCity(params, function(res) {
                                _this.setState({ cityList: res.data });
                                if (!res) {
                                    cityEle.setData([]);
                                } else {
                                    cityEle.setData(res);
                                }
                            });
                        }
                    },
                    onSearchValueChange: function(attachEvent, data, currentPage) {
                        ent_authService.getAllCountry({ q: data, pageSize: 10, pageIndex: currentPage ? currentPage : 1 }, function(res) {
                            if (res.errorCode === 0) {
                                res.data.forEach(function(item) {
                                    item.nameCode = item.name + '(' + item.code + ')';
                                });
                                attachEvent.setData(res);
                                _this.setState({ countryList: res.data });
                            }
                        });
                    }
                });
                var cityEle = selectFactory({
                    data: [],
                    id: 'city',
                    isSearch: true,
                    pagination: true,
                    showTextField: 'name',
                    defaultText: '请选择',
                    searchPlaceHoder: '请输入城市名称',
                    closeLocalSearch: true,
                    onSearchValueChange: function(attachEvent, data, currentPage) {
                        var params = {
                            q: data,
                            pageIndex: currentPage ? currentPage : 1,
                            countryCode: _this.state.countryCode
                        };
                        ent_authService.getAllCity(params, function(res) {
                            _this.setState({ cityList: res.data });
                            if (res.errorCode !== 0) {
                                attachEvent.setData([]);
                            } else {
                                attachEvent.setData(res);
                            }
                        });
                    },
                    attrTextModel: function(name) {
                        _this.refs.locationCityCode.setValue(name);
                    }
                });
                countryEle.next = cityEle;
            },
            getAllCurrency: function() {
                var _this = this;
                selectFactory({
                    data: [],
                    id: 'currency',
                    closeLocalSearch: true,
                    isSearch: true,
                    showTextField: 'nameCode',
                    searchPlaceHoder: '请输入币种名称或者三字码',
                    defaultText: '请选择',
                    pagination: true,
                    attrTextModel: function(nameCode) {
                        _this.refs.tradingCurrency.setValue(nameCode);
                    },
                    onSearchValueChange: function(attachEvent, data, currentPage) {
                        ent_authService.getAllCurrency({ q: data ? data.trim() : '', pageIndex: currentPage ? currentPage : 1, pageSize: 10 }, function(res) {
                            if (res.data.length) {
                                res.data.forEach(function(item) {
                                    item.nameCode = item.name + '(' + item.code + ')';
                                });
                                attachEvent.setData(res);
                                _this.setState({ currencyList: res.data });
                            } else {
                                attachEvent.setData({data: []});
                                _this.setState({ currencyList: [] });
                            }
                        });
                    }
                });
            },
            getAllClearing: function() {
                var _this = this;
                ent_authService.getAllClearing({ pageSize: 100 }, function(res) {
                    if (!res.data) return;
                    _this.setState({ clearingList: res.data });
                    selectFactory({
                        data: res,
                        id: 'clearing',
                        offset: -300,
                        isSearch: false,
                        defaultText: '请选择',
                        defaultCount: 100,
                        attrTextModel: function(name) {
                            _this.refs.refSettlementId.setValue(name);
                        }
                    });
                });
            },
            validEmail: function(e) {
                var value = e.target.value;
                if (value === '') {
                    this.setState({ emailError: false });
                    this.setState({ email: value });
                    return;
                }
                var _this = this;
                this.setState({ email: value });
                if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value)) {
                    this.setState({ emailError: true, emailErrorTxt: '电子邮箱格式不正确' });
                    return;
                } else {
                    ent_authService.checkEmail({ email: value }, function(res) {
                        if (res.errorCode !== 0) {
                            _this.setState({
                                emailError: true,
                                emailErrorTxt: '电子邮箱已存在'
                            });
                        } else {
                            _this.setState({ emailError: false });
                        }
                    });
                }
            },
            showResult: function(e) {
                this.setState({ mainBusiness: e });
            },
            submitFalse: function(data) {
                PromptComponent.init({ type: 'warning', msg: '请填写所有数据后再提交' }).show();
                var email = this.state.authInfo.email || this.state.email;
                if (!email) {
                    this.setState({ emailError: true, emailErrorTxt: '' });
                }
                if (email && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
                    this.setState({
                        emailError: true,
                        emailErrorTxt: '电子邮箱格式不正确'
                    });
                }
                var imgList = this.muploads.getImgInfo();
                if (!imgList.length) {
                    this.setState({ imgError: true, imgErrorTxt: '营业执照必须上传' });
                }
            },
            submitOk: function(data) {
                var email = this.state.authInfo.email || this.state.email;
                if (!email || !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
                    this.setState({
                        emailError: true,
                        emailErrorTxt: '电子邮箱格式不正确'
                    });
                    return;
                } else {
                    this.setState({ emailError: false });
                }
                var imgList = this.muploads.getImgInfo();
                if (!imgList.length) {
                    this.setState({ imgError: true, imgErrorTxt: '营业执照必须上传' });
                    return;
                } else {
                    this.setState({ imgError: false, imgErrorTxt: '(最多上传9张图片, 单张图片最大10M)' });
                    var attachFileIds = imgList.map(function(item) {
                        return item.id;
                    });
                    data.attachFileIds = this.unique(attachFileIds);
                }
                if (this.state.countryList.length) {
                    this.state.countryList.forEach(function(item) {
                        if (item.nameCode === data.locationCountryCode) data.locationCountryCode = item.code;
                    });
                } else {
                    data.locationCountryCode = this.state.authInfo.locationCountryCode;
                }
                if (this.state.cityList.length) {
                    this.state.cityList.forEach(function(item) {
                        if (item.name === data.locationCityCode) data.locationCityCode = item.id;
                    });
                } else {
                    data.locationCityCode = this.state.authInfo.locationCityCode;
                }
                if (this.state.currencyList) {
                    this.state.currencyList.forEach(function(item) {
                        if (item.nameCode === data.tradingCurrency) data.tradingCurrency = item.code;
                    });
                } else {
                    data.tradingCurrency = this.state.authInfo.tradingCurrency;
                }
                if (this.state.clearingList) {
                    this.state.clearingList.forEach(function(item) {
                        if (item.name === data.refSettlementId) data.refSettlementId = item.id;
                    });
                } else {
                    data.refSettlementId = this.state.authInfo.refSettlementId;
                }
                data.mainBusiness = this.state.mainBusiness ? this.state.mainBusiness : '';
                data.email = email;
                this.refs.form.setFormPristine();
                var _this = this;
                ent_authService.confirmChange(data, function(res) {
                    if (res.errorCode === 0) {
                        _this.setState({ submitSuccess: true });
                    } else {
                        PromptComponent.init({ type: 'warning', msg: res.msg }).show();
                    }
                });
            },
            // 数组去重
            unique: function(arr) {
                var ret = [];
                var hash = {};
                for (var i = 0; i < arr.length; i++) {
                    var item = arr[i];
                    var key = typeof item + item;
                    if (hash[key] !== 1) {
                        ret.push(item);
                        hash[key] = 1;
                    }
                }
                return ret;
            },
            render: function() {
                return ~init;
            }
        });
        if (typeof ReactDOM != 'undefined') {
            ReactDOM.render(<PageComponent data={storage} />, document.getElementById('container'));
        }
        module.exports = PageComponent;
    }
);
